package oas.web;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import oas.db.LeaveRepository;
import oas.db.LeaveTypeRepository;
import oas.db.UserLeaveRepository;
import oas.domain.Attendance;
import oas.domain.Leave;
import oas.domain.LeaveType;
import oas.domain.User;

/**
 * 员工请假控制器
 * @author Dzq
 *
 */
@Controller
@RequestMapping("/leave")
public class UserLeaveController {
	
	@Autowired
	private UserLeaveRepository userLeaveRepository;
	
	@Autowired
	private LeaveTypeRepository leaveTypeRepository;
	
	private static Logger logger = Logger.getLogger("oas");
	
	/**
	 * 请假页面
	 * 
	 * @return 请假页面视图
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String showUserLeave(Model model) {
		List<LeaveType> list = leaveTypeRepository.findAll();
		model.addAttribute("list", list);
		return "userLeaves";
	}
	
	/**
	 * 处理申请请假
	 * @param session
	 * @param request
	 * @return 申请成功页面视图
	 */
	@RequestMapping(method = RequestMethod.POST)
	public String applyLeave(HttpSession session, HttpServletRequest request) {
		User user = (User) session.getAttribute("user");
		User handler = null;
		long id = 1;
		String LTI = request.getParameter("reason");
		long leaveTypeId = Long.parseLong(LTI);
		Date applyTime = new Date();
		Date handleTime = null;
		String status = "0";
		LeaveType leaveType = new LeaveType(leaveTypeId,"",true);
		Leave leave = new Leave(id,user,applyTime,handler,handleTime,status,leaveType);
		leave = userLeaveRepository.save(leave);
		return "applyLeaveSuccess";
	}
	
	/**
	 * 分页显示个人请假历史记录
	 * @param pageNo 起始页面
	 * @param pageSize 页面大小
	 * @param model 前端使用的model
	 * @param session
	 * @return 个人历史请假页面视图
	 */
	@RequestMapping(value = "/history", method = RequestMethod.GET)
	public String showUserLeaveHistory(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize, Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		PaginationSupport<Leave> ps = userLeaveRepository.findPage(pageNo, pageSize, user);
		logger.debug(ps.getItems().toString());
		model.addAttribute("paginationSupport", ps);
		
		return "userLeavesHistory";
	}
	
	/**
	 * 查看某一个人某一请假详情
	 * @param id
	 * @param session
	 * @param model
	 * @return 请假详情页面视图
	 */
	@RequestMapping(value = "/history/{id}", method = GET)
   	public String showOneLeave(@PathVariable("id") Long id ,HttpSession session,Model model) {
   		model.addAttribute(userLeaveRepository.findById(id));
   		return "leaveMessage";  
   	}
	
}
